import Request from '@/api/http/request.ts'
import useUserStore from '@/store/user.ts'

const request = new Request({
	baseURL: import.meta.env.VITE_APP_URL,
	timeout: import.meta.env.VITE_TIME_OUT,
	msgField: 'msg',
	headers: {
		'Content-Type': 'application/json;charset=utf-8'
	},
	interceptors: {
		requestInterceptors: (config) => {
			const userStore = useUserStore()
			const token: string = userStore.getToken
			const refreshToken: string = userStore.getFreshToken
			if (token) {
				config.headers.Authorization = 'Bearer ' + (config.headers.refreshToken ? refreshToken : token)
			}
			return config
		},
		responseInterceptors: (res) => {
			return res
		}
	}
})
export default request
